/**
 * Created by lixiaodi on 2018/10/10.
 */
mini.parse();
var grid = mini.get("datagrid1");
grid.load();        //加载成功以后显示数据
// grid.sortBy("createtime", "desc");  //排序
var url = bootPATH + "../frame/frame1/pages/";
//增加
function add() {
    mini.open({
        url: url + "alertBox/EmployeeWindow.html",
        title: "新增员工", width: 600, height: 400,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: "new"};
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
            //通过action的值判断子页面的关闭方式
            grid.reload();
        }
    });
}
//编辑
function edit() {
    //得到被选中的行数据
    var row = grid.getSelected();
    if (row) {
        mini.open({
            url: url + "alertBox/EmployeeWindow.html",
            title: "编辑员工", width: 600, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                //通过action判断是编辑还是增加
                var data = { action: "edit", id: row.userId };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                grid.reload();
            }
        });

    } else {
        mini.alert("请选中一条记录");
    }

}
//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        if (mini.confirm("确定删除选中记录？")) {
            var ids = [];
            for (var i = 0, l = rows.length; i < l; i++) {
                var r = rows[i];
                ids.push(r.userId);
            }
            var id = ids.join(',');
            grid.loading("操作中，请稍后......");
            $.ajax({
                url: ajaxUrl + "/user/deleteUserInfo.do?userIds=" + id,
                type:"post",
                success: function (text) {
                    grid.reload();
                },
                error: function () {
                }
            });
        }
    } else {
        mini.alert("请选中一条记录");
    }
}
//查询
function search() {
    var key = mini.get("key").getValue();
    //grid.load({ key: key });

    $.ajax({
        url: ajaxUrl + "/user/vagueUserName.do",
        type:"post",
        datatype:"json",
        data:{
            userName:key
        },
        success: function (data) {
            grid.setData(data.data)     //将查询到的数据显示到数据表中
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
function onKeyEnter(e) {
    search();
}
/////////////////////////////////////////////////
function onBirthdayRenderer(e) {
    var value = e.value;
    if (value) return mini.formatDate(value, 'yyyy-MM-dd');
    return "";
}
var Genders = [{ id: 1, text: '男' }, { id: 2, text: '女'}];
function onGenderRenderer(e) {
    for (var i = 0, l = Genders.length; i < l; i++) {
        var g = Genders[i];
        if (g.id == e.value) return g.text;
    }
    return "";
}

function refresh(){
    grid.reload();
}
function close(){

}

